<?php
/** */
require_once('sr23_pdo.php');

$sr23 = new sr23_pdo(); // create an object from class

// start the HTML
?>
<html>
  <head>
    <title></title>
    <style type="text/css">
      body {
          background: #eee;
          font-family: sans-serif;

      }
      th, td {
          border: solid 0.1em #aaa;
      }  
      th {
          text-align: right;
          width: 270px;
      }

      td {
          width: 300px;
      }
    </style>
  </head>
  <body>
    
    <div id="search_descriptions">
      <!--
      <form method="post" action="search_oo.php">
        <table>
          <tr class="request_row">
            <th>NDB_No:</th>
            <td><input type="text" name="ndbno[]" value="" size="5" maxlength="5"/></td>
            <th>weight:</th>
            <td><input type="text" name="weight[]" value="" size="5" maxlength="8"/></td>
            <td><button class="add">add</button></td>
            <td><button class="remove">remove</button></td>
          </tr>  
        </table>
        <p><input type="submit" value="calculate"/></p>
      </form>
      -->
      <p>
        If you do not know the NDB_No for the food you are looking for, type a series of words that can be used to describe the food or nutrient you are looking for below.
      </p>
      <form method="post" action="search_oo.php">
        <table>
          <tr>
            <th>Search For Description:</th>
            <td>
              <input type="text" name="description" value="" size="45" maxlength="150"/>
            </td>
          </tr>
          <tr>
            <th>Maximum Number of Matches:</th>
            <td>
              <input type="text" name="show_max" value="25" size="3" maxlength="3"/>
            </td>
          </tr>
          <tr>
            <th colspan="2">      
              <input type="hidden" name="stype" value="description"/>
              <input type="submit" value="find description"/>
            </th>
          </tr>  
        </table>
      </form>
    </div>
    <div id="search_by_NDB_No">
      <p>
        To search for a known NDB number and weight (in grams), enter in search string this way <i>01001:123</i>, 
        where the first 5 digit number is the NDB Number and numbers following the colon is the weight in grams.
        To search for multiples, separate each of the stringed examples by a semi-colon, like this: <i>01001:123;01002:23</i>.
      </p>
      <form method="POST" action="search_oo.php">
        <table>
          <tbody>
            <tr>
              <th>search for NDB_No (and weight):</th>
              <td>
                <input name="search_string" size="35" maxlength="255"/>
              </td>
            </tr>
            <tr>
              <th colspan="2">
                <input type="hidden" name="stype" value="ndbno_weight"/>
                <input type="submit" value="search"/>
              </th>
            </tr>
          </tbody>
        </table>
      </form>
    </div>
    <div id="results">
<?php
// results go here
// distinguish search type
if ($_POST) {
    switch ($_POST['stype']) {
        case 'ndbno_weight':
        // otherwise process the search_string, check and see if there are semi-colons and colons
        $patt_single = '/\d{5}:\d+/';

        if (preg_match_all($patt_single, $_POST['search_string'], $matches)) 
        {
            print '<p>NDB_No:weight search query accepted.</p>';
            foreach ($matches[0] as $match) {
                // break the pair
                $ndbno = '~' . substr($match, 0,5) . "~";
                $weight = substr($match, 6,3);
                print "<p>NDB_No: $ndbno, at $weight grams</p>";     
                $results = $sr23->calculate_nutrient_from_weight($ndbno, $weight, $sr23a_keys);
                print '<pre>';
                print_r($results);
                print '</pre>';
            }
            
            // take the single ndbno and weight and do a search
        }  
        else
        {
            // try to see if there is anything in here that is salvagable
            // print '<p>searching for NDB_No searches only.</p>';
            // separate out the NDB_Nos and do a basic search
            if (preg_match_all('/\d{5}/', $_POST['search_string'], $matches)) {
                /*
                // test the matches array
                print '<pre>';
                print_r($matches);
                print '</pre>';
                */

                // print out a form to include weight
                // out put as list for each match
                if (count($matches[0]) > 0) {
                    foreach ($matches[0] as $match) {
                        $sr23->print_nutrient($match);
                    }
                }
                else
                {
                    print '<p>Your search query returns no matches.</p>';
                }
            }
        }
        break;
        case 'description':
        $desc = $_POST['description'];
        print '<p>search descriptions: ' . stripslashes($desc) . '</p>';  
        $sr23->match_description(stripslashes($desc));
        /*
        // create PDO
        try
        {
            $dbh = new PDO('sqlite:sr23.db');
        }
        catch (PDOException $e) {
            print $e->getMessage();
        }            
        
        // check to see if search description has quotes, if so, search as single words, otherwise do a search for each word
        if (preg_match("/^\"/", $desc, $matches)) {
            // strip the quotes
            $desc = str_replace('"','',$desc); // get rid of quotes
            // die($desc);
            // single word search
            $query = "select NDB_No, Long_Desc from food_description where Long_Desc like '%$desc%' or Long_Desc like '%$desc' or Long_Desc like '$desc%'";   
            $sth = $dbh->prepare($query);
            $sth->execute();
            $results = $sth->FetchAll(PDO::FETCH_ASSOC);
            if (count($results) > $_POST['show_max']) {
                print '<p>there are more than ' . $_POST['show_max'] . ' matches (' . count($results) . ' found).</p>';
            }
            else
            {
                print_pretty_array($results);
            }
        }
        else
        {
            // multi word search
            // break apart by space     
            $words = explode(" ", $desc); // split apart by space
            // die(print_r($words));
            foreach ($words as $desc) {
                $query = "select NDB_No, Long_Desc from food_description where Long_Desc like '%$desc%' or Long_Desc like '%$desc' or Long_Desc like '$desc%'"; 
                $sth = $dbh->prepare($query);
                $sth->execute();
                $results = $sth->FetchAll(PDO::FETCH_ASSOC);
                if (count($results) > $_POST['show_max']) {
                    print '<p>there are more than ' . $_POST['show_max'] . ' matches (' . count($results) . ' found for description <b>' . $desc . '</b>).</p>';
                }
                else
                {
                    print_pretty_array($results);
                }
            }
        }
        */
        break;
    }
}
?>
  </body>
</html>  
        
